package com.rd.p2p.app.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.model.AppCodeModel;
import com.qdlc.p2p.biz.service.NoticeService;
import com.qdlc.p2p.biz.service.user.UserRegistService;
import com.qdlc.p2p.biz.util.ValidateUtil;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.constant.NoticeTplNid;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.common.util.PropertiesUtil;
import com.qdlc.p2p.common.util.RandomUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.model.UserModel;

public class AppGetCodeAction extends BaseAction<AppCodeModel> implements ModelDriven<AppCodeModel> {

	@Resource
	private NoticeService noticeService;

	@Resource
	private UserRegistService userRegistService;

	private Map<String, Object> data;

	private static final String SESSION_REGIST_USER_MODEL = "regist_user_model";

	/**
	 * 获取验证码
	 * 
	 * @throws IOException
	 */
	@Action("/app/common/getCode")
	public void getCode() throws IOException {
		data = model.validParam();
		if (StringUtil.isBlank(data)) {
			data = new HashMap<String, Object>();
			Map<String, Object> sendData = new HashMap<String, Object>();
			if (StringUtil.equals(model.getType(), NoticeTplNid.PHONE_SEND)) { // 通过手机--发送验证码
				sendData.put("phone", model.getReceiveAddr());
				String code = RandomUtil.code();
				request.getSession().setAttribute(model.getNoticeId() + model.getReceiveAddr() + "_code", code);
				noticeService.sendNotice(model.getReceiveAddr(), NoticeTplNid.PHONE_SEND, model.getNoticeId(), code);
				data.put("resCode", "1");
				data.put("resMsg", PropertiesUtil.getValue("AppGetCodeAction04"));
				printWebJson(getStringOfJpaObj(data));
			}
			if (StringUtil.equals(model.getType(), NoticeTplNid.EMAIL_SEND)) { // 通过邮箱--发送验证码
				sendData.put("email", model.getReceiveAddr());
				String code = RandomUtil.code();
				request.getSession().setAttribute(model.getNoticeId() + model.getReceiveAddr() + "_code", code);
				noticeService.sendNotice(model.getReceiveAddr(), NoticeTplNid.EMAIL_SEND, model.getNoticeId(), code);
				data.put("resCode", "1");
				data.put("resMsg", PropertiesUtil.getValue("AppGetCodeAction05"));
				printWebJson(getStringOfJpaObj(data));
			}
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppGetCodeAction06"));
			printWebJson(getStringOfJpaObj(data));
		} else {
			printWebJson(getStringOfJpaObj(data));
		}
	}

	/**
	 * 发送手机验证码
	 * 
	 * @throws IOException
	 */
	@Action("/app/common/getPhoneCode")
	public void getPhoneCode() throws IOException {
		data = new HashMap<String, Object>();
		String mobilePhone = paramString("mobilePhone");
		if (StringUtils.isBlank(mobilePhone) || !ValidateUtil.isPhone(mobilePhone)) {
			data.put("resCode", "0");
			data.put("resMsg", "手机号码格式不正确");
		}
		// 验证码
		//2. 开始发送注册短信
		if (StringUtil.isBlank(data)) {
			ReturnResult result = new ReturnResult();
			UserModel userModel = (UserModel) session.get(SESSION_REGIST_USER_MODEL);
			result.addExtra(SESSION_REGIST_USER_MODEL, userModel);
			mobilePhone = StringUtils.trimToEmpty(paramString("mobilePhone"));
			result = userRegistService.doSendValidCode(userModel, mobilePhone);
			if(result.isError()){
				data.put("resCode","0");
				data.put("resMsg",result.getErrorInfo());
				printWebJson(getStringOfJpaObj(data));
				return;
			}
			data.put("resCode", "1");
			data.put("resMsg", "发送成功");
			printWebJson(getStringOfJpaObj(data));
		} else {
			printWebJson(getStringOfJpaObj(data));
		}
	}

	/**
	 * 校验验证码
	 * 
	 * @throws IOException
	 */
	@Action("/app/common/checkCode")
	public void checkCode() throws IOException {
		data = model.validParams();
		if (StringUtil.isBlank(data)) {
			data = model.checkResponse();
			printJson(getStringOfJpaObj(data));
		} else {
			printJson(getStringOfJpaObj(data));
		}
	}
}
